package com.wuyi.controller.admin;

import com.github.pagehelper.PageInfo;
import com.wuyi.pojo.Category;
import com.wuyi.pojo.bo.AdminAddCategoryBO;
import com.wuyi.pojo.bo.AdminCategoryBO;
import com.wuyi.service.PostService;
import com.wuyi.service.admin.AdminService;
import com.zc.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName AdminController.java
 * @Description 用于后台管理的相关接口
 * @createTime 2021年10月15日 21:24:00
 */
@Api(value = "后台管理",tags = {"用于后台管理的相关接口"})
@RestController
@RequestMapping("admin")
public class AdminController {


    @Autowired
    private AdminService adminService;
    @Autowired
    private PostService postService;


    /*@ApiOperation(value = "分类模块-条件查询（可调试）", notes = "分类模块-条件查询（可调试）", httpMethod = "GET")
    @GetMapping("/category/query")
    public JSONResult queryCategory(@ApiParam(name = "categoryName", value = "分类名称", required = false)
                                    @RequestParam String categoryName,
                                    @ApiParam(name = "categoryDesc", value = "分类描述", required = false)
                                    @RequestParam String categoryDesc) {
        List<Category> categories = adminService.queryCategory(categoryName, categoryDesc);
        return JSONResult.ok(categories);
    }
    *//**
     * 分类模块-新增
     * @param categoryBO
     * @return
     *//*
    @ApiOperation(value = "分类模块-新增（可调试）", notes = "分类模块-新增（可调试）", httpMethod = "POST")
    @PostMapping("/category/add")
    public JSONResult editCategory(@RequestBody AdminAddCategoryBO categoryBO) {

        if (StringUtils.isBlank(categoryBO.getCategoryName())) {
            return JSONResult.errorMsg("分类名称不能为空");
        }
        if (StringUtils.isBlank(categoryBO.getCategoryDesc())) {
            return JSONResult.errorMsg("分类描述不能为空");
        }
        return adminService.addCategory(categoryBO);
    }

        *//**
         * 分类模块-修改
         * @param categoryBO
         * @return
         *//*
    @ApiOperation(value = "分类模块-修改（可调试）", notes = "分类模块-修改（可调试）", httpMethod = "POST")
    @PostMapping("/category/edit")
    public JSONResult editCategory(@RequestBody AdminCategoryBO categoryBO) {

        if (StringUtils.isBlank(categoryBO.getCategoryName())) {
            return JSONResult.errorMsg("分类名称不能为空");
        }
        if (StringUtils.isBlank(categoryBO.getCategoryDesc())) {
            return JSONResult.errorMsg("分类描述不能为空");
        }
        return adminService.editCategory(categoryBO);
    }


    @ApiOperation(value = "分类模块-删除（可调试）", notes = "分类模块-删除（可调试）", httpMethod = "POST")
    @PostMapping("/category/delete")
    public JSONResult delCategory(@ApiParam(name = "categoryId", value = "分类Id", required = true)
                                  @RequestParam Integer categoryId) {

        if (null == categoryId) {
            return JSONResult.errorMsg("分类不存在");
        }

        return adminService.delCategory(categoryId);
    }*/


    @ApiOperation(value = "用户管理-条件查询（可调试）", notes = "用户管理-条件查询（可调试）", httpMethod = "GET")
    @GetMapping("/user/query")
    public JSONResult queryUser(@RequestParam( required = false) String userName,
                                @RequestParam( required = false) Integer userRole,
                                @RequestParam( required = false,defaultValue = "1") Integer pageNum,
                                @RequestParam( required = false,defaultValue = "10") Integer pageSize) {

        PageInfo pageInfo = adminService.queryUser(userName, userRole, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @ApiOperation(value = "用户管理-删除用户（可调试）", notes = "用户管理-删除用户（可调试）", httpMethod = "GET")
    @GetMapping("/user/delete")
    public JSONResult deleteUser(@RequestParam String userId) {

        if (null  == userId) return JSONResult.errorMsg("userId不能为空");
        return JSONResult.ok(adminService.deleteUser(userId));
    }


    @ApiOperation(value = "帖子管理-条件查询（可调试）", notes = "帖子管理-条件查询（可调试）", httpMethod = "GET")
    @GetMapping("/post/query")
    public JSONResult queryPost(@ApiParam(name = "postTitle", value = "帖子标题", required = false)
                                @RequestParam String postTitle,
                                @ApiParam(name = "userName", value = "发布人名称", required = false)
                                @RequestParam String userName,
                                @ApiParam(name = "postTime", value = "发布时间", required = false)
                                @RequestParam String postTime,
                                @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam( required = false,defaultValue = "10")Integer pageSize) throws ParseException {

        PageInfo pageInfo = adminService.queryPost(postTitle, userName, postTime, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }


    @ApiOperation(value = "帖子管理-删除帖子（可调试）", notes = "帖子管理-删除帖子（可调试）", httpMethod = "GET")
    @GetMapping("/post/delete")
    public JSONResult deletePost(@ApiParam(name = "postId", value = "帖子Id", required = true)
                                 @RequestParam Integer postId) {

        if (null  == postId) return JSONResult.errorMsg("postId不能为空");
        postService.deletePost(postId);
        return JSONResult.ok();
    }


    @ApiOperation(value = "帖子管理-条件查询（可调试）", notes = "帖子管理-条件查询（可调试）", httpMethod = "GET")
    @GetMapping("/resource/query")
    public JSONResult queryResource(@ApiParam(name = "resourceName", value = "资源名称", required = false)
                                @RequestParam String resourceName,
                                @ApiParam(name = "userName", value = "发布人名称", required = false)
                                @RequestParam String userName,
                                @ApiParam(name = "resourceTime", value = "发布时间", required = false)
                                @RequestParam String resourceTime,
                                @ApiParam(name = "categoryId", value = "分类Id 1：为资料  2：为课件  3：为软件", required = true)
                                @RequestParam Integer categoryId,
                                @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam( required = false,defaultValue = "10")Integer pageSize) throws ParseException {

        if (null == categoryId) return JSONResult.errorMsg("categoryId不能为空");
        PageInfo pageInfo = adminService.queryResource(resourceName, userName,
                                                        resourceTime, categoryId,
                                                        pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }










}
